angular.module("staffApp",['ui.bootstrap','httpService','tm.pagination']).
controller("staffCtrl",['$scope','$http','$uibModal','$timeout','staffService','departmentService',function($scope,$http,$modal,$timeout,staffService,departmentService){
    $scope.staffs=[];
    $scope.checkedIndex = -1;
    $scope.selectChange=function(index){
       if($scope.staffs[index].checked==true){
            $scope.staffs[index].checked=false;
          }
       else{
            $scope.staffs[index].checked=true;
           }
        if($scope.staffs[index].checked){
            $scope.checkedIndex = index;
            return;
        }
        for(var i = 0; i < $scope.staffs.length; i++){
            var item = $scope.staffs[i];
            if(item.checked != undefined && item.checked == true){
                $scope.checkedIndex = i;
                return;
            }
        }
        $scope.checkedIndex = -1;
  };
    departmentService.getAll().then(function (response) {
      $scope.departments=response;
    });
    $scope.addStaff=function(){
        $modal.open({
            templateUrl:'modifyStaff',  
            scope:$scope,         
            controller:"insertStaffCtrl"
        });
    };
  
    $scope.searchByName=function(){
        if ($scope.nameKeywords=="") {
            alert("请输入姓名关键字");
            return;
        };
        staffService.getStaffInfoByKeyWords({keywords:$scope.nameKeywords}).then(function(response){
             $scope.staffs=response;
      //       $scope.paginationConf.totalItems=response.length;
        });
    };
    $scope.departmentChange=function(id){
        if(id==null){
            return;
        }
        staffService.getStaffInfoByDepartment({departmentId:id}).then(function(response){
             $scope.staffs=response;
    //         $scope.paginationConf.totalItems=response.length;
        });
        
    };
    $scope.updateStaff=function(){
        if($scope.checkedIndex==-1){
            alert("请选择");
            return;
        }
        $modal.open({
          templateUrl:'modifyStaff',
          scope:$scope,
          controller:'updateStaffCtrl'
      });
    };
     $scope.paginationConf = {
            currentPage: 1,
            totalItems: 8000,
            itemsPerPage: 10,
            pagesLength: 10,
            perPageOptions: [10, 20, 30, 40, 50],
            rememberPerPage: 'perPageItems',
            onChange: function(){
                
            }
        };
     $scope.getStaffCount=function(){
         staffService.getStaffCount().then(function(response){
             $scope.paginationConf.totalItems=response;
         });
     };
    $scope.refreshStaff=function(){
         $scope.getStaffCount();
         staffService.getStaffInfoByCount({start:($scope.paginationConf.currentPage-1)*$scope.paginationConf.itemsPerPage,count:$scope.paginationConf.itemsPerPage}).then(function(result){
         $scope.staffs=result;
        });
    };
    $scope.$watch('paginationConf',function(){$scope.refreshStaff();},true);  
    $scope.deleteStaff=function(){
         if($scope.checkedIndex==-1){
            alert("请选择");
            return;
        }
       if(window.confirm("确实要删除吗")){
           staffService.delete({id:$scope.staffs[$scope.checkedIndex].id}).then(function(){
              $scope.refreshStaff();
              alert("已经删除"); 
           });
       }
    };
}])
.controller("insertStaffCtrl",['$scope','$uibModalInstance',"staffService",function($scope,$modalInstance,staffService){
    staffService.getDepartment().then(function(result){
        $scope.departments=result;
      //  $scope.department=result[0];
    });
    $scope.isInitPassword=false;
    $scope.mstaff={};
    $scope.staffIsEabled=[{id:true,Eabled:'可用'},{id:false,Eabled:'冻结'}];
    $scope.sexes=[{id:1,sex:'男'},{id:0,sex:'女'}];
    $scope.levels=[{id:1,name:'科员'},{id:2,name:'科级'},{id:3,name:'处级'}]; 
    $scope.statuses=[{id:'0',status:'正式职工'},{id:'1',status:'招聘'},{id:'2',status:'劳务派遣'},{id:'3',status:'退休返聘'},{id:'4',status:'离退休'}];
    $scope.submit=function(){
        $scope.loading=true;
        $scope.mstaff.password="666666";
        staffService.insertByAdmin($scope.mstaff).then(function(){
            $scope.submitted=true;
            $scope.loading=false;
            $scope.result="成功";
            $scope.refreshStaff();
        },function(){
            $scope.submitted=true;
            $scope.loading=false;
            $scope.result="失败";
        },function(){
            $scope.submitted=true;
            $scope.loading=false;
            $scope.result="失败";
        });
    };
    $scope.close=function(){
    $modalInstance.close('ok');
    };
}]).
controller("updateStaffCtrl",['$scope','$uibModalInstance',"staffService",function($scope,$modalInstance,staffService){
    $scope.staffIsEabled=[{id:true,Eabled:'可用'},{id:false,Eabled:'冻结'}];
    $scope.sexes=[{id:1,sex:'男'},{id:0,sex:'女'}];
    $scope.levels=[{id:1,name:'科员'},{id:2,name:'科级'},{id:3,name:'处级'}]; 
    $scope.statuses=[{id:'0',status:'正式职工'},{id:'1',status:'招聘'},{id:'2',status:'劳务派遣'},{id:'3',status:'退休返聘'},{id:'4',status:'离退休'}];
    $scope.isInitPassword=true;
    staffService.getDepartment().then(function(result){
        $scope.departments=result;
    
    });  
    $scope.mstaff=$scope.staffs[$scope.checkedIndex];
    $scope.initPassword=function(){
        staffService.initPassword({id:$scope.mstaff.id,password:$scope.mstaff.password}).then(function(response){
                    alert($scope.mstaff.name+"初始化密码成功!");
        },function(){
                    alert($scope.mstaff.name+"初始化密码失败!");
        });
    };
    $scope.submit=function(){
        delete $scope.mstaff.checked;
        delete $scope.mstaff.departmentname;
        
        $scope.loading=true;
        staffService.updateByAdmin($scope.mstaff).then(function(result){
            $scope.submitted=true;
            $scope.loading=false;
            $scope.result="成功";
            $scope.refreshStaff();
        });
    };
    $scope.close=function(){
    $modalInstance.close('ok');
    };
}]);
